#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef __linux__
    #define pause "read -p \"Press <Enter> to continue ...\" tmpVar"
#elif _WIN32
    #define pause "pause"
#elif __APPLE__
    #define pause "echo -e \"Press any key to continue ...\" && read -n 1"
#endif

typedef FILE* File;

typedef struct {
    int num;
    char name[20];
    double score;
}ONE;

typedef struct {
    int ID;
    char name[20];
    char sex;
    int age;
    double score;
}TWO;

typedef struct {
    int num;
    char name[20];
    char sex;
    double score;
}THREE;

void firstReport();
void secondReport();
void thirdReport();

int main() {
    firstReport();
    system(pause);
    secondReport();
    system(pause);
    thirdReport();
    return 0;
}

void firstReport() {
    File writeFile;
    writeFile = fopen("file1.dat", "w");
    if(writeFile != NULL) {
        int i;
        ONE stuInfo[10];
        for(i = 0; i < 10; i ++) {
            printf("Enter the No.%2d <Num> <Name> <Score>:\t", i + 1);
            scanf("%d %s %lf", &stuInfo[i].num, stuInfo[i].name, &stuInfo[i].score);
            fprintf(writeFile, "%d %s %.1lf\n", stuInfo[i].num, stuInfo[i].name, stuInfo[i].score);
        }
        fclose(writeFile);
    } else {
        printf("Error! Cannot write file \"file1.dat\"!\n");
        exit(-1);
    }
}

void secondReport() { //Used the info from LAB8.c
    int i, j;
    const short N = 10; //#define N 10
    TWO stuList[N], tmpStruct;
    TWO *pList = stuList;
    int IDList[] = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
    char nameList[][8] = {"Zhang", "Wang", "Zhao", "Li", "Gao", "Lin", "Ma", "Zhen", "Xu", "Mao"};
    char sexList[] = {'M', 'F', 'M', 'M', 'M', 'M', 'F', 'M', 'M', 'F'};
    int ageList[] = {19, 18, 19, 20, 19, 18, 18, 21, 19, 22};
    double scoreList[] = {95.6, 92.2, 85.7, 96.3, 90.2, 91.2, 98.7, 88.7, 90.1, 94.7};
    for(i = 0; i < N; i ++) {
        (*(pList + i)).ID = IDList[i];
        strcpy((*(pList + i)).name, nameList[i]);
        (*(pList + i)).sex = sexList[i];
        (*(pList + i)).age = ageList[i];
        (*(pList + i)).score = scoreList[i];
    }
    File writeFile;
    writeFile = fopen("file2.dat", "w");
    if(writeFile != NULL) {
        for(i = 0; i < 10; i ++) {
            fprintf(writeFile, "%d %s %c %d %.1lf\n", stuList[i].ID, stuList[i].name, stuList[i].sex, stuList[i].age, stuList[i].score);
        }
        fclose(writeFile);
    } else {
        printf("Error! Cannot write file \"file2.dat\"!\n");
        exit(-1);
    }
    File readFile, writeFileWithText, writeFileWithBits;
    readFile = fopen("file2.dat", "r");
    writeFileWithText = fopen("file3.dat", "w");
    writeFileWithBits = fopen("file4.dat", "wb");
    if((readFile != NULL) && (writeFileWithText != NULL) && (writeFileWithBits != NULL)) {
        TWO stuInfo[10];
        for(i = 0; i < N; i ++) {
            fscanf(readFile, "%d %s %c %d %lf\n", &stuInfo[i].ID, stuInfo[i].name, &stuInfo[i].sex, &stuInfo[i].age, &stuInfo[i].score);
            fprintf(writeFileWithText, "%d %s %c %d %.1lf\n", stuInfo[i].ID, stuInfo[i].name, stuInfo[i].sex, stuInfo[i].age, stuInfo[i].score);
            fprintf(writeFileWithBits, "%d %s %c %d %.1lf\n", stuInfo[i].ID, stuInfo[i].name, stuInfo[i].sex, stuInfo[i].age, stuInfo[i].score);
        }
    } else {
        printf("Error! Cannot open data file!\n");
        exit(-1);
    }
}

void thirdReport() {
    int i, j;
    THREE stuInfo[145], tmpInfo;
    File readFile, writeFile;
    readFile = fopen("student.dat", "rb");
    writeFile = fopen("students.dat", "wb");
    if((readFile != NULL) && (writeFile != NULL)) {
        for(i = 0; i < 145; i ++) {
            fread(&stuInfo[i], 1, sizeof(stuInfo), readFile);
        }
        for(i = 0; i < 145; i ++) {
            for(j = i + 1; j < 145; j ++) {
                if(stuInfo[i].score < stuInfo[j].score) {
                    tmpInfo = stuInfo[i];
                    stuInfo[i] = stuInfo[j];
                    stuInfo[j] = tmpInfo;
                }
            }
        }
        printf("ID\tName\tSex\tScore\n");
        for(i = 0; i < 145; i ++) {
            fwrite(&stuInfo[i], sizeof(stuInfo[i]), 1, writeFile);
            printf("%3d\t%s\t%c\t%.1lf\n", stuInfo[i].num, stuInfo[i].name, stuInfo[i].sex, stuInfo[i].score);
        }
    } else {
        printf("Error! Cannot open data file!\n");
        exit(-1);
    }
    fclose(readFile);
    fclose(writeFile);
} 

